package com.ruoyi.project.datamanagement.controller;

import java.io.FileWriter;
import java.nio.channels.ReadPendingException;
import java.util.*;

import com.ruoyi.anan.util.ZhihesjUtil;
import com.ruoyi.project.api.service.SelectService;
import com.ruoyi.project.datamanagement.service.ISysSearchTreeService;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.datamanagement.domain.SysDataSearch;
import com.ruoyi.project.datamanagement.service.ISysDataSearchService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 查询管理Controller
 *
 * @author anan
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/datamanagement/dataSearch")
public class SysDataSearchController extends BaseController
{
    @Autowired
    private ISysDataSearchService sysDataSearchService;

    @Autowired
    private ISysSearchTreeService sysSearchTreeService;

    @Autowired
    private SelectService selectService;

    /**
     * 查询查询管理列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDataSearch sysDataSearch)
    {
        startPage();
        List<SysDataSearch> list = sysDataSearchService.selectSysDataSearchList(sysDataSearch);
        return getDataTable(list);
    }

    /**
     * 查询查询管理列表根据ancestors
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:list')")
    @GetMapping("/getListByAncestors")
    public TableDataInfo getListByAncestors(SysDataSearch sysDataSearch)
    {
        startPage();
        List<SysDataSearch> list = sysDataSearchService.getListByAncestors(sysDataSearch);
        return getDataTable(list);
    }

    /**
     * 导出查询管理列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:export')")
    @Log(title = "查询管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDataSearch sysDataSearch)
    {
        List<SysDataSearch> list = sysDataSearchService.selectSysDataSearchList(sysDataSearch);
        ExcelUtil<SysDataSearch> util = new ExcelUtil<SysDataSearch>(SysDataSearch.class);
        return util.exportExcel(list, "search");
    }

    /**
     * 获取查询管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataSearch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDataSearchService.selectSysDataSearchById(id));
    }

    /**
     * 新增查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:add')")
    @Log(title = "查询管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDataSearch sysDataSearch)
    {
        return toAjax(sysDataSearchService.insertSysDataSearch(sysDataSearch));
    }

    /**
     * 修改查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:edit')")
    @Log(title = "查询管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDataSearch sysDataSearch)
    {
        return toAjax(sysDataSearchService.updateSysDataSearch(sysDataSearch));
    }

    /**
     * 删除查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:remove')")
    @Log(title = "查询管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataSearchId}")
    public AjaxResult remove(@PathVariable Long dataSearchId)
    {
        return toAjax(sysDataSearchService.deleteSysDataSearchById(dataSearchId));
    }

    /**
     * 查询管理
     * @param map
     * @return
     */
    @GetMapping("/selectMethod")
    public AjaxResult selectMethod(@RequestParam Map map){
        String method = (String) map.get("method");
        SysDataSearch dataSearch = new SysDataSearch();
        List list1 = new ArrayList();
        if (!method.isEmpty()) {
            /** 获取查询语句 */
            dataSearch = selectService.getDataSourceByMethod(method);
            /** 根据查询语句获取结果 */
            List list = selectService.getData(dataSearch,map);
            /** 将后面获取的list转换成驼峰list */
            list1 = ZhihesjUtil.getConResults(list);
        }
        return AjaxResult.success(list1);
    }

}